package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.service.CheckItemService;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController //等于Controller+ResponseBody,下面方法不用再写ResponseBody
@RequestMapping("/checkItem")
public class CheckItemController {

   @Reference
   private CheckItemService checkItemService;

   /**
    * 添加检查项
    *
    * @param checkItem
    * @return
    */
   @RequestMapping("/add")
   //@ResponseBody
   public Result add(@RequestBody CheckItem checkItem) {//解释@RequestBody:前端发送数据我json对象,故使用
      //System.out.println(checkItem);//测试
      //Result result = new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);//测试
      try {
         checkItemService.add(checkItem);
      } catch (Exception e) {
         e.printStackTrace();
         //添加失败
         return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
      }
      //添加成功
      return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);

   }

   /**
    * 分页查询
    *
    * @param pageBean
    * @return
    */
   @RequestMapping("/findPage")
   public PageResult findPage(@RequestBody QueryPageBean pageBean) {
      //1.调用checkItemService的findPage方法
      PageResult pageResult = checkItemService.findPage(pageBean);
      return pageResult;
   }

   /**
    * 删除检查项
    * @param id
    * @return
    */
   @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
   @RequestMapping("/delete/{id}")
   public Result delete(@PathVariable Integer id) {
      //调用checkItemService的delete方法
      try {
         checkItemService.delete(id);
      } catch (Exception e) {
         e.printStackTrace();
         //报错,则返回错误信息
         return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
      }
      //删除成功
      return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
   }

   /**
    * 通过id查询
    * @param id
    * @return
    */
   @RequestMapping("/findById/{id}")
   public Result findById(@PathVariable Integer id) {
      CheckItem checkItem =null;
      try {
          checkItem = checkItemService.findById(id);
      } catch (Exception e) {
         e.printStackTrace();
         return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
      }
      return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
   }

   /**
    * 更新
    * @param checkItem
    * @return
    */
   @RequestMapping("/update")
   public Result update(@RequestBody CheckItem checkItem) {
      //1.调用checkItemService的update方法
      try {
         checkItemService.update(checkItem);

      } catch (Exception e) {
         e.printStackTrace();
         //修改失败
         return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
      }
      return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
   }

   /**
    * 查询所有
    * @return
    */
   @RequestMapping("/findAll")
   public Result findAll() {
      //调用checkItemService的findAll方法
      List<CheckItem> checkItemList =null;
      try {
         checkItemList = checkItemService.findAll();
      } catch (Exception e) {
         e.printStackTrace();
         //查询失败
         return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
      }
      //查询成功
      return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
   }


}
